﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


public interface IShape
{
    double CalculateArea(); // 计算面积
    bool IsValid(); // 判断形状是否合法
}

// 定义抽象形状类
public abstract class Shape : IShape
{
    public abstract double CalculateArea(); // 计算面积的抽象方法
    public abstract bool IsValid(); // 判断形状是否合法的抽象方法
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double CalculateArea()
    {
        return Length * Width;
    }

    public override bool IsValid()
    {
        return Length > 0 && Width > 0;
    }
}

// 正方形类
public class Square : Rectangle
{
    public Square(double sideLength) : base(sideLength, sideLength)
    {
    }
}

// 三角形类
public class Triangle : Shape
{
    public double BaseLength { get; set; }
    public double Height { get; set; }

    public Triangle(double baseLength, double height)
    {
        BaseLength = baseLength;
        Height = height;
    }

    public override double CalculateArea()
    {
        return 0.5 * BaseLength * Height;
    }

    public override bool IsValid()
    {
        return BaseLength > 0 && Height > 0;
    }
}

class ConsoleApp2
{
    static void Main()
    {
        // 测试长方形
        Rectangle rectangle = new Rectangle(5, 4);
        if (rectangle.IsValid())
        {
            Console.WriteLine("长方形的面积为：" + rectangle.CalculateArea());
        }
        else
        {
            Console.WriteLine("长方形不合法！");
        }

        // 测试正方形
        Square square = new Square(5);
        if (square.IsValid())
        {
            Console.WriteLine("正方形的面积为：" + square.CalculateArea());
        }
        else
        {
            Console.WriteLine("正方形不合法！");
        }

        // 测试三角形
        Triangle triangle = new Triangle(3, 4);
        if (triangle.IsValid())
        {
            Console.WriteLine("三角形的面积为：" + triangle.CalculateArea());
        }
        else
        {
            Console.WriteLine("三角形不合法！");
        }
    }
}
